package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private DeptService deptService;

    @Autowired
    private EmpExprMapper empExprMapper;

    /**
     * 分页查询
     * @param
     * @param
     * @return
     */
    @Override
    public PageResult findPage(EmpQueryParam param) {
        //2设置分页条件
        IPage<Emp> pageinfo = new Page<>(param.getPage(),param.getPageSize());

        LambdaQueryWrapper<Emp> queryWrapepr =
                Wrappers.<Emp>lambdaQuery()
                        .like(param.getName() != null,Emp::getName, param.getName())
                        .eq(param.getGender() != null,Emp::getGender, param.getGender())
                        .between(param.getBegin() != null && param.getEnd() != null,Emp::getEntryDate, param.getBegin(), param.getEnd());
        //3设置排序条件
        queryWrapepr.orderByDesc(Emp::getUpdateTime);

        List<Emp> empList = null;//获取当前页数据
        long total = 0;//获取总记录数


            //1调用分页方法
            IPage<Emp> pageResult = this.page(pageinfo, queryWrapepr);

            empList = pageResult.getRecords();
            total = pageResult.getTotal();

        if (!CollectionUtils.isEmpty(empList)) {
            //3.1从emplist中获取对应的部门id集合
            List<Integer> deptIdList = (List<Integer>) empList.stream().map(emp -> emp.getDeptId()).toList();
            //3.2 去重
            deptIdList = deptIdList.stream().distinct().toList();
            //3.3根据部门id集合查询部门名称列表
            List<Dept> deptList = deptService.listByIds(deptIdList);
            //3.4封装部门名称列表到empList中
            empList.forEach(emp -> {
                deptList.stream().filter(dept -> dept.getId().equals(emp.getDeptId()))
                        .findFirst()//获取部门名称,返回值对象
                        .ifPresent(dept -> emp.setDeptName(dept.getName()));//如果有之，进行赋值操作
            });
        }


        return new PageResult(total,empList);
    }

    /**
     * 新增员工
     * @param emp
     */
    @Override
    @Transactional
    public void saveEmp(Emp emp) {
        //1.新增员工信息表emp
        emp.setPassword("123456");
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        this.save(emp);
        //2.批量新增工作经历表
        List<EmpExpr> exprList = emp.getExprList();

        if (!CollectionUtils.isEmpty(exprList)) {
            //3.给工作经历对象赋值empid
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));

            empExprMapper.insert(exprList);
        }

    }
}
